/**
 * @author Flower
 * @description Common Util
 */

  let loadingModal = undefined;
  let initLoadingModal = function(){
    loadingModal = document.createElement("div");
    loadingModal.setAttribute( 'id', 'loadingModal' );
    loadingModal.setAttribute( 'class', 'weui_loading_toast' );
    loadingModal.innerHTML = `<div class="weui_mask_transparent"></div>
      <div class="weui_toast">
      <div class="weui_loading">
      <div class="weui_loading_leaf weui_loading_leaf_0"></div>
      <div class="weui_loading_leaf weui_loading_leaf_1"></div>
      <div class="weui_loading_leaf weui_loading_leaf_2"></div>
      <div class="weui_loading_leaf weui_loading_leaf_3"></div>
      <div class="weui_loading_leaf weui_loading_leaf_4"></div>
      <div class="weui_loading_leaf weui_loading_leaf_5"></div>
      <div class="weui_loading_leaf weui_loading_leaf_6"></div>
      <div class="weui_loading_leaf weui_loading_leaf_7"></div>
      <div class="weui_loading_leaf weui_loading_leaf_8"></div>
      <div class="weui_loading_leaf weui_loading_leaf_9"></div>
      <div class="weui_loading_leaf weui_loading_leaf_10"></div>
      <div class="weui_loading_leaf weui_loading_leaf_11"></div>
      </div>
      <p class="weui_toast_content">数据加载中</p>
      </div>`;
  };

export default {
    isObject: input => (typeof input === 'object') && !(input instanceof Array),

    isEmptyObj:  input => {
        let isEmpty = true;
        Object.keys( input ).forEach( key =>{
          isEmpty = false;
        }) 
        return isEmpty;
    },
    encodeHtml : function(str){
      if(!str) return '';
      let arrEntities={'lt':'<','gt':'>','nbsp':' ','amp':'&','quot':'"'};
      return str.replace(/&(lt|gt|nbsp|amp|quot);/ig,(all,t)=>arrEntities[t]);
    },
    loading: function(){
      if( !loadingModal ){
        initLoadingModal();
      };
      var closeLoading = function(){
        var model = document.getElementById('loadingModal');
        if( model ){
          document.body.removeChild(loadingModal);
        }
      };
      var startLoading = function(){
        var model = document.getElementById('loadingModal');
        if( !model ){
          document.body.appendChild(loadingModal);
        }
      };
      var isLoading = function(){
        return !!document.getElementById('loadingModal');
      }
      return{
        start: startLoading,
        close: closeLoading,
        isLoading: isLoading
      }
    },
    popup: function( opts ){
      if( !this.isObject(opts) ) opts = {};
      var dispearTime = opts.time || 1000,
      //default
      content = opts.content || '已完成',
      //default icon for toast
      icon = opts.icon || '';
      // var defer = $.Deferred();
      var modal = document.createElement("div");
      modal.setAttribute( 'id', 'loadingToast' );
      modal.setAttribute( 'class', 'weui_loading_toast' );
      modal.innerHTML = `<div id="toast" >
          <div class="weui_mask_transparent"></div>
          <div class="weui_toast error_message">
              <p class="weui_toast_content" id="toastContent"></p>
          </div>
      </div>`;
      document.body.appendChild(modal);
      $( '#toastContent' ).html(content);
      $( '#toastIcon' ).addClass(icon);
        setTimeout(function(){
          document.body.removeChild(modal);
        },dispearTime)
    },
}

/**
 * Deep copy the given object considering circular structure.
 * This function caches all nested objects and its copies.
 * If it detects circular structure, use cached copy to avoid infinite loop.
 *
 * @param {*} obj
 * @param {Array<Object>} cache
 * @return {*}
 */
export function deepCopy (obj, cache = []) {
  // just return if obj is immutable value
  if (obj === null || typeof obj !== 'object') {
    return obj
  }

  // if obj is hit, it is in circular structure
  const hit = find(cache, c => c.original === obj)
  if (hit) {
    return hit.copy
  }

  const copy = Array.isArray(obj) ? [] : {}
  // put the copy into cache at first
  // because we want to refer it in recursive deepCopy
  cache.push({
    original: obj,
    copy
  })

  Object.keys(obj).forEach(key => {
    copy[key] = deepCopy(obj[key], cache)
  })

  return copy
}
